﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using System.Data;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;

namespace Runda.CMS.Portal.Budget
{
    public partial class SalesbudgetDetail : BasePage
    {
        #region property
        public Guid? AssistID
        {
            get
            {
                if (hidAssistID.Value != "")
                {
                    return new Guid(hidAssistID.Value);
                }
                if (Request.QueryString["AssistID"] != null)
                {
                    return new Guid(Request.QueryString["AssistID"]);
                }
                return null;
            }
            set { hidAssistID.Value = value.Value.ToString(); }
        }

        public override bool IsCheckAccessible
        {
            get
            {
                return false;
            }
        }
        #endregion

        #region Page Methods

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitializeControls();

                if (AssistID.HasValue) //修改状态
                {
                    AssistID = AssistID;
                    InitializeFormData();
                    ddlBudgetYear.Enabled = false;
                }
                else //新增状态
                {
                    AssistID = Guid.NewGuid();
                    ddlProducter.SelectedValue = "";
                    ddlSales.SelectedValue = "";
                    ddlBudgetYear.SelectedValue = "2013";
                    ddlBudgetYear.Enabled = false;
                }

                BindGrid1();
                BindGrid3();
                BindGrid2();
            }
        }

        #endregion

        #region private methods

        private void InitializeControls()
        {
            SystemBll bll = new SystemBll();
            List<SysUserEntity> list = bll.GetAllUser();
            ddlSales.DataSource = list;
            ddlSales.DataTextField = "UserName";
            ddlSales.DataValueField = "UserName";
            ddlSales.DataBind();
            ddlSales.Items.Add(new ListItem("   ", ""));

            DataTable dt = new SystemBll().GetUsersByRole(100);
            ddlProducter.DataSource = dt;
            ddlProducter.DataTextField = "username";
            ddlProducter.DataValueField = "username";   
            ddlProducter.DataBind();
            ddlProducter.Items.Add(new ListItem("   ", ""));

            dt = new SystemBll().GetUsersByRole(101);
            ddlProductManager.DataSource = dt;
            ddlProductManager.DataTextField = "username";
            ddlProductManager.DataValueField = "username";
            ddlProductManager.DataBind();
            if (!base.CheckUserHasFuction(1, "Func10011")) { this.lnkbtnSaveAndClose.Visible = false; }
            

            //制单人必须为当前登录者
            ddlCreater.Items.Add(new ListItem(CurrentUserEntity.UserName, CurrentUserID.ToString()));
            ddlProducter.SelectedValue = CurrentUserEntity.UserName;
            ddlProductManager.SelectedValue = CurrentUserEntity.UserName;
            for (int i = 2012; i <= 2015; i++)
            {
                ddlBudgetYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            
        }

        private void InitializeFormData()
        {
            BudgetBll bll = new BudgetBll();
            BGSalesBudgetsEntity entity = bll.Get<BGSalesBudgetsEntity>(" and AssistID ='"+AssistID.Value+"'");
            if (entity != null)
            {
                ddlcompany.SelectedValue = entity.company;
                hidCustomerID.Value = entity.customerID.ToString();
                CtrtCustomerEntity CustomerEnt = bll.Get<CtrtCustomerEntity>(entity.customerID);
                if (CustomerEnt != null)
                    txtCustomer.Text = CustomerEnt.CustomerName;
                if(entity.CustomerIsTrue == "Y")
                    chbCustomerIsTrue.Checked = true;

                ddlProducter.SelectedValue = entity.Producter==null ? "" : entity.Producter;
                ddlProductManager.SelectedValue = entity.ProductManager;
                ddlSales.SelectedValue = entity.sales;
                ddlCreater.SelectedValue = entity.biller;
                ddlBudgetYear.SelectedValue = entity.BudgetYear;
                txtMemo.Text = entity.MemoEx;

                if (new BudgetBll().GetBudgetSubmitStatus(ddlBudgetYear.SelectedValue.ToString(), entity.ProductManager) > 0)
                { lnkbtnSaveAndClose.Visible = false; }

                ddlcompany.Enabled = false;
                txtCustomer.Enabled = false;
                ddlProducter.Enabled = false;
                ddlProductManager.Enabled = false;
                ddlSales.Enabled = false;
                ddlCreater.Enabled = false;
                ddlBudgetYear.Enabled = false;
                txtMemo.Enabled = false;
            }
        }

        private void BindGrid1()
        {
            BudgetBll bll = new BudgetBll();

            DataTable dt = bll.GetSalesBudgets(" and AssistID = '"+AssistID.Value+"' and SalesBudgetType='1' and ProductType='1' ");
            gridAccessoryList.DataSource = dt;
            gridAccessoryList.DataBind(); 
        }

        private void BindGrid3()
        {
            BudgetBll bll = new BudgetBll();

            DataTable dt = bll.GetSalesBudgets(" and AssistID = '" + AssistID.Value + "' and SalesBudgetType='1' and ProductType='2' ");
            gridAccessoryList1.DataSource = dt;
            gridAccessoryList1.DataBind();
        }

        private void BindGrid2()
        {
            BudgetBll bll = new BudgetBll();

            DataTable dt = bll.GetSalesBudgets(" and AssistID = '" + AssistID.Value + "' and SalesBudgetType='2' ");
            gridAccessoryList2.DataSource = dt;
            gridAccessoryList2.DataBind();
        }
        #endregion

        #region Event Methods

        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            if (ddlSales.SelectedValue == "")
            { ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('请指定销售责任人!');", true);
            return;
            }
            BudgetBll bll = new BudgetBll();
            BGSalesBudgetsEntity entity = new BGSalesBudgetsEntity();

            entity.AssistID = AssistID.Value;
            entity.BudgetYear = ddlBudgetYear.SelectedValue;
            entity.company = ddlcompany.SelectedValue;
            entity.customerID = new Guid(hidCustomerID.Value);
            entity.CustomerIsTrue = chbCustomerIsTrue.Checked ? "Y" : "N";
            entity.Producter = ddlProducter.SelectedValue;
            entity.sales = ddlSales.SelectedValue;
            entity.biller = ddlCreater.SelectedValue;
            entity.MemoEx = txtMemo.Text;
            entity.ProductManager = ddlProductManager.SelectedValue;

            bll.UpdateByAssistID(entity);
            ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功!');window.close();window.opener.document.getElementById('ctl00_cphBody_btnRefersh').click();", true);
        }

        protected void btnRefersh1_Click(object sender, EventArgs e)
        {
            BindGrid1();
        }

        protected void btnRefersh2_Click(object sender, EventArgs e)
        {
            BindGrid2();
        }

        protected void btnRefersh3_Click(object sender, EventArgs e)
        {
            BindGrid3();
        }

        protected void gridAccessoryList_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "delete")
            {
                BudgetBll bll = new BudgetBll();
                bll.DeleteByBudgetID(new Guid(e.CommandArgument.ToString()));
                BindGrid1();
            }
        }
        protected void gridAccessoryList_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                LinkButton lnkbtnEdit = e.Item.FindControl("lnkbtnEdit") as LinkButton;
                LinkButton lnkbtnDelete = e.Item.FindControl("lnkbtnDelete") as LinkButton;
                DataRowView dv = e.Item.DataItem as DataRowView;

                if (new BudgetBll().GetBudgetSubmitStatus(ddlBudgetYear.SelectedValue.ToString(), dv["ProductManager"].ToString()) > 0 || !base.CheckUserHasFuction(1, "Func10011"))
                { //lnkbtnEdit.Visible = false; 
                    lnkbtnDelete.Visible = false; }
                else
                {
                    lnkbtnEdit.Attributes.Add("onclick", "openMyWin('SalesBudgetSaleEdit_1.aspx?BudgetID=" + dv["BudgetID"].ToString() + "&customername=" + dv["customerName"].ToString() + "&BudgetYear=" + dv["BudgetYear"].ToString() + "', 'SalesBudgetSaleEdit', 800, 600);return false;");
                }
                lnkbtnEdit.Attributes.Add("onclick", "openMyWin('SalesBudgetSaleEdit_1.aspx?BudgetID=" + dv["BudgetID"].ToString() + "&customername=" + dv["customerName"].ToString() + "&BudgetYear=" + dv["BudgetYear"].ToString() + "', 'SalesBudgetSaleEdit', 800, 600);return false;");

                
            }
        }

        protected void gridAccessoryList1_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "delete")
            {
                BudgetBll bll = new BudgetBll();
                bll.DeleteByBudgetID(new Guid(e.CommandArgument.ToString()));
                BindGrid1();
            }
        }
        protected void gridAccessoryList1_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                LinkButton lnkbtnEdit = e.Item.FindControl("lnkbtnEdit") as LinkButton;
                LinkButton lnkbtnDelete = e.Item.FindControl("lnkbtnDelete") as LinkButton;

                DataRowView dv = e.Item.DataItem as DataRowView;
                if (new BudgetBll().GetBudgetSubmitStatus(ddlBudgetYear.SelectedValue.ToString(), dv["ProductManager"].ToString()) > 0 || !base.CheckUserHasFuction(1, "Func10011"))
                { //lnkbtnEdit.Visible = false; 
                    lnkbtnDelete.Visible = false; }
                else
                {
                    lnkbtnEdit.Attributes.Add("onclick", "openMyWin('SalesBudgetSaleEdit_2.aspx?BudgetID=" + dv["BudgetID"].ToString() + "&customername=" + dv["customerName"].ToString() + "&BudgetYear=" + dv["BudgetYear"].ToString() + "', 'SalesBudgetSaleEdit_2', 800, 600);return false;");

                }
                lnkbtnEdit.Attributes.Add("onclick", "openMyWin('SalesBudgetSaleEdit_2.aspx?BudgetID=" + dv["BudgetID"].ToString() + "&customername=" + dv["customerName"].ToString() + "&BudgetYear=" + dv["BudgetYear"].ToString() + "', 'SalesBudgetSaleEdit_2', 800, 600);return false;");
            }
        }

        protected void gridAccessoryList2_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "delete")
            {
                BudgetBll bll = new BudgetBll();
                bll.DeleteByBudgetID(new Guid(e.CommandArgument.ToString()));
                BindGrid1();
            }
        }
        protected void gridAccessoryList2_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                LinkButton lnkbtnEdit = e.Item.FindControl("lnkbtnEdit") as LinkButton;
                DataRowView dv = e.Item.DataItem as DataRowView;
                LinkButton lnkbtnDelete = e.Item.FindControl("lnkbtnDelete") as LinkButton;

                if (new BudgetBll().GetBudgetSubmitStatus(ddlBudgetYear.SelectedValue.ToString(), dv["ProductManager"].ToString()) > 0 || !base.CheckUserHasFuction(1, "Func10011"))
                { 
                    //lnkbtnEdit.Visible = false; 
                    lnkbtnDelete.Visible = false; }
                else
                {
                    lnkbtnEdit.Attributes.Add("onclick", "openMyWin('SalesBudgetPutEdit.aspx?BudgetID=" + dv["BudgetID"].ToString() + "&BudgetYear=" + dv["BudgetYear"].ToString() + "&customername=" + dv["customerName"].ToString() + "', 'SalesBudgetPutEdit', 800, 600);return false;");
                }

                lnkbtnEdit.Attributes.Add("onclick", "openMyWin('SalesBudgetPutEdit.aspx?BudgetID=" + dv["BudgetID"].ToString() + "&BudgetYear=" + dv["BudgetYear"].ToString() + "&customername=" + dv["customerName"].ToString() + "', 'SalesBudgetPutEdit', 800, 600);return false;");
            }
        }
       

        #endregion
    }
}
